package News.Web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import News.Dao.NewsDao;
import News.Dao.TopicDao;
import News.entity.Newsu;
import News.entity.Page;
import News.entity.Topic;

public class Shouye extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Shouye() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		TopicDao topicdaoone = new TopicDao();
		Topic topicone = topicdaoone.findOne(1);
		request.setAttribute("topicone", topicone);
		NewsDao newsdao = new NewsDao();
		List<Newsu> listone = newsdao.findTop(1);
		request.setAttribute("listone", listone);
		TopicDao topicdaotwo = new TopicDao();
		Topic topictwo = topicdaotwo.findOne(2);
		request.setAttribute("topictwo", topictwo);
		List<Newsu> listtwo = newsdao.findTop(2);
		request.setAttribute("listtwo", listtwo);
		TopicDao topicdaothree = new TopicDao();
		Topic topicthree = topicdaothree.findOne(1);
		request.setAttribute("topicthree", topicthree);
		newsdao = new NewsDao();
		List<Newsu> listthree = newsdao.findTop(5);
		request.setAttribute("listthree", listthree);
		TopicDao topicdao = new TopicDao();
		List<Topic> listtopic = topicdao.findAll();
		request.setAttribute("listtopic", listtopic);
		int pageNo = 1;
		Integer tid = null;
		try {
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		} catch (Exception e) {

		}
		try {
			tid = Integer.parseInt(request.getParameter("tid"));

		} catch (Exception e) {
		}
		String qstr = (tid == null ? "" : "&tid=" + tid);

		newsdao = new NewsDao();
		Page<Newsu> pagenews = newsdao.find(pageNo, tid);
		List<Newsu> listnews = pagenews.getDatas();
		int numb = pagenews.getTotalPages();
		request.setAttribute("listnews", listnews);
		request.setAttribute("pageNo", pageNo);
		request.setAttribute("qstr", qstr);
		request.setAttribute("numb", numb);
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
